package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.ResultComparator;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeList;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeStatisticsService;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;

public class StatisticsGcsImpl implements EmployeeStatisticsService {
    @Override
    public ResultList pivotByGender(EmployeeList employees) {
        ResultList resultList = new ResultListGcsImpl(employees.size());
        for (int i = 0; i < employees.size(); i++) {
            Employee emp = employees.get(i);
            Result r = resultList.findByKey(emp.getGender());
            if (null == r) {
                resultList.add(new Result(emp.getGender(), 1));
            } else {
                r.setCount(r.getCount() + 1);
            }
        }

        return resultList;
    }

    @Override
    public ResultList pivotByDepartment(EmployeeList employees) {
        ResultList resultList = new ResultListGcsImpl(employees.size());
        for (int i = 0; i < employees.size(); i++) {
            Employee emp = employees.get(i);
            Result r = resultList.findByKey(emp.getDepartment());
            if (null == r) {
                resultList.add(new Result(emp.getDepartment(), 1));
            } else {
                r.setCount(r.getCount() + 1);
            }
        }
        // Count 正排序
        resultList.sort(new ResultComparator() {
            @Override
            public int compare(Result r1, Result r2) {
                return r1.getCount() - r2.getCount();
            }
        });
        return resultList;
    }

    @Override
    public ResultList pivotByJobTitle(EmployeeList employees) {
        ResultList resultList = new ResultListGcsImpl(employees.size());
        for (int i = 0; i < employees.size(); i++) {
            Employee emp = employees.get(i);
            Result r = resultList.findByKey(emp.getJobTitle());
            if (null == r) {
                resultList.add(new Result(emp.getJobTitle(), 1));
            } else {
                r.setCount(r.getCount() + 1);
            }
        }
        // Count 倒排序
        resultList.sort(new ResultComparator() {
            @Override
            public int compare(Result r1, Result r2) {
                return r2.getCount() - r1.getCount();
            }
        });
        return resultList;
    }
}
